package com.example.demo;

import com.example.demo.model.SysRole;
import com.example.demo.model.User;

import com.example.demo.repo.SysRoleRepository;
import com.example.demo.repo.UserRepository;
import com.example.demo.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MVCConfig implements WebMvcConfigurer {
    private Logger logger= LoggerFactory.getLogger(MVCConfig.class);
    @Autowired
   private UserRepository userRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
   private SysRoleService sysRoleService;
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");

        registry.addViewController("/hello").setViewName("hello");
        //registry.addViewController("/login").setViewName("login");



        initSysRole();


        initTestData();

    }

    private void initSysRole() {


        //终极管理员，拥有修改删除所有信息权限
        SysRole ultimateSysRole=new SysRole();
        //管理员
        SysRole adminSysRole=new SysRole();
        //普通用户
        SysRole userSysRole=new SysRole();



        adminSysRole.setName("ROLE_ADMIN");
        userSysRole.setName("ROLE_USER");
        ultimateSysRole.setName(("ROLE_ULTIMATE"));

        if(!sysRoleRepository.existsSysRoleByName(adminSysRole.getName()))
        {
            logger.info("admin role has not been existed");
            sysRoleRepository.save(adminSysRole);
        }
        else {
            logger.info("admin role has  been existed");

        }
        if(!sysRoleRepository.existsSysRoleByName(userSysRole.getName()))
        {
            logger.info("user role has not been existed");

            sysRoleRepository.save(userSysRole);
        }
        else {
            logger.info("user role has tbeen existed");

        }

        if(!sysRoleRepository.existsSysRoleByName(ultimateSysRole.getName()))
        {
            logger.info("ultimate role has not been existed");

            sysRoleRepository.save(ultimateSysRole);
        }
        else {
            logger.info("ultimate role has  been existed");

        }


    }

    private void initTestData() {
        User user= new User();
        user.setUsername("test001");
        user.setName("Tom");
        user.setPassword(new BCryptPasswordEncoder().encode("123456"));

        if(!userRepository.existsUserByUsername(user.getUsername()))
        {
            user= userRepository.save(user);
            List<SysRole> roles1=new ArrayList<>();
            roles1.add(sysRoleService.getUserSysRole());
            user.setSysRoles(roles1);
            SysRole userRole=sysRoleService.getUserSysRole();
            userRepository.save(user);
        }

        User adminUlt=new User();
        User admin002=new User();//普通管理员
        admin002.setUsername("admin002");
        admin002.setName("Tom2");
        admin002.setPassword(new BCryptPasswordEncoder().encode("123456"));

        if(!userRepository.existsUserByUsername(admin002.getUsername())){
            List<SysRole> roles=new ArrayList<>();
            roles.add(sysRoleService.getAdminSysRole());
            admin002=userRepository.save(admin002);
            admin002.setSysRoles(roles);
            userRepository.save(admin002);
        }





        adminUlt.setName("ultimate");
        adminUlt.setUsername("adminult");
        adminUlt.setPassword(new BCryptPasswordEncoder().encode("123456"));

        if(!userRepository.existsUserByUsername(adminUlt.getUsername()))
        {
            adminUlt=userRepository.save(adminUlt);

            List<SysRole> roles=new ArrayList<>();
            roles.add(sysRoleService.getAdminSysRole());
            roles.add(sysRoleService.getUltimateSysRole());

            adminUlt.setSysRoles(roles);

            userRepository.save(adminUlt);
        }

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedMethods("PUT","POST","GET","DELETE").exposedHeaders("Authorization");

        registry.addMapping("/users/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedMethods("PUT","POST","GET","DELETE").exposedHeaders("Authorization");

    }



}